<?php

namespace App\Http\Controllers\RbhMiniProgram;

use App\Exceptions\BusinessException;
use App\Http\Controllers\Controller;
use App\Models\CustomerModel;
use App\Models\FavModel;
use App\Models\GoodsPicPrimaryModel;
use App\Models\RdsUserStoreModel;
use App\Models\SupplyModel;
use App\Utils\ApiResponse;
use App\Utils\VerifyRequestInput;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class MiniProgramStoreController extends Controller
{
    use ApiResponse, VerifyRequestInput;

    /**
     * 获取收藏列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getGoodsList(Request $request): JsonResponse
    {
        $paginate = RdsUserStoreModel::getModel($request)->orderByDesc('id')->paginate(10);
        $items = json_decode(json_encode($paginate->items()));
        $list = [];
        $goodsCollection = GoodsPicPrimaryModel::whereIn('ID', array_column($items, 'goods_id'))
            ->select('ID', 'PName', 'Path', 'FName')->get();
        foreach ($items as $item) {
            $storeGoodsItem = $goodsCollection->where('ID', $item->goods_id)->first();
            if ($storeGoodsItem)
                $storeGoodsItem->url = self::getImageFullUrl($storeGoodsItem, true);
            $item->goods  = $storeGoodsItem;
            $list[] = $item;
        }
        return $this->success(['total' => $paginate->total(), 'list' => $list]);
    }

    /**
     * 获取收藏商家列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getSupplyList(Request $request): JsonResponse
    {
        $paginate = RdsUserStoreModel::getModel($request)->orderByDesc('id')->paginate(10);
        $items = json_decode(json_encode($paginate->items()));
        $list = [];
        $supplyCollection = SupplyModel::whereIn('ID', array_column($items, 'sid'))
            ->select('ID', 'CName', 'Manager', 'Mobile')->get();
        foreach ($items as $item) {
            $item->supply  = $supplyCollection->where('ID', $item->sid)->first();;
            $list[] = $item;
        }
        return $this->success(['total' => $paginate->total(), 'list' => $list]);
    }

    /**
     * 收藏
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function doSave(Request $request): JsonResponse
    {
        $uid = $this->verifyId('uid');
        if ($request->input('goods_id') )
            $item = RdsUserStoreModel::where('goods_id', $request->input('goods_id'))->where('uid', $uid)->first();
        else
            $item = RdsUserStoreModel::where('sid', $request->input('sid'))->where('uid', $uid)->first();
        $this->doAsyncDeleteOrCreate($item, $request);
        return $this->success();
    }

    /**
     * 异步删除或添加
     * @param $item
     * @param $request
     * @return void
     */
    private function doAsyncDeleteOrCreate($item, $request): void
    {
        if ($item)
            $item->delete();
        else
            RdsUserStoreModel::create([
                'uid'       => $request->input('uid'),
                'sid'       => $request->input('sid', 0),
                'goods_id'  => $request->input('goods_id', 0),
                'date'      => Carbon::today()->toDateString()
            ]);
        // 发送异步消息
    }

}
